package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 封装Response
 * 该对象需要提供核心办法，输出html
 */
public class Response {
    private OutputStream outputStream;

    public Response() {

    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * 输出指定字符串
     */
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }


    public void outputHtml(String path) throws IOException {
        if ("".equals(path) || path == null) {
            path = "index.html";
        }
        String absoluteResourcePath = path.replace("\\\\", "/");

        File file = new File(absoluteResourcePath);
        if (file.exists() && file.isFile()) {
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        } else {
            output(HttpProtocolUtil.getHttpHeader404());
        }

    }
}
